package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import code.KeyStoreManager;
import code.Language;
import code.Settings;

public class KeyDeletion extends JPanel {

	Color generate = new Color(59, 89, 152);
	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JLabel jLabel = null;
//	private JLabel jLabel1 = null;
//	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	
	private HashMap labels;
	ATOCOmain atocoframe;
	private KeyStoreManager ksm;
	private int count;
	private JLabel jLabel1 = null;
	private Settings s;

	/**
	 * This is the default constructor
	 */
	public KeyDeletion(ATOCOmain atocoframe) {
		super();
		s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "KeyDeletion", (String) s.global.get("installPath"));
		this.atocoframe=atocoframe;
		
		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager(
				(String)Settings.global.get("personalKeyStoreFile"), 
				(String)Settings.global.get("personalKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
        	System.out.println("Keystore file not found. Creating one.");
        	ksm.createKeyStore(
        			(String)Settings.global.get("personalKeyStoreFile"), 
    				(String)Settings.global.get("personalKeyStorePassword"));
        }
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(83, 355, 378, 16));
		jLabel3 = new JLabel();
		jLabel3.setBounds(new Rectangle(25, 14, 379, 30));
		jLabel3.setText((String) labels.get("jLabel3"));
//		jLabel2 = new JLabel();
//		jLabel2.setBounds(new Rectangle(505, 178, 128, 35));
//		jLabel2.setText("Export Private Key");
//		jLabel1 = new JLabel();
//		jLabel1.setBounds(new Rectangle(505, 127, 126, 32));
//		jLabel1.setText("Export Public Key");
		jLabel = new JLabel((String) labels.get("jLabel"), SwingConstants.CENTER);
		jLabel.setBounds(new Rectangle(500, 85, 123, 29));
		jLabel.setBackground(Color.RED);
		jLabel.setForeground(Color.WHITE);
		jLabel.setOpaque(true);
		jLabel.setFont(new Font("Arial", Font.BOLD, 14));
		jLabel.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				ksm.deleteKeyPair(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
				ksm.saveKeyStore();
				atocoframe.contentPane(new KeyDeletion(atocoframe));
			}
		});
		this.setSize(745, 431);
		this.setLayout(null);
		this.add(getJScrollPane(), null);
		this.add(jLabel, null);
//		this.add(jLabel1, null);
//		this.add(jLabel2, null);
		this.add(jLabel3, null);
		this.setBackground(Color.WHITE);
		this.add(jLabel1, null);
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(67, 70, 351, 263));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			
			Vector<Object> rowData = new Vector<Object>();
			
			HashSet<String> aliases = ksm.listAllKeys();
			Iterator it = aliases.iterator();
			while(it.hasNext()) {
				String al = (String) it.next();
				String type = null;
				if (ksm.getPublicKey(al) == null) {
	        		// No certificate entry so it must be secretKey
					type = "Secret Key";
				} else {
					type = "Asymetric Key";
				}
				Vector<Object> row = new Vector<Object>();
				row.add(al);
				row.add(type);
//						ksm.getCertificate(al).toString());
//				row.add(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nexticonsmall.gif"));
				rowData.add(row);
			}
			//Object rowData[][] = { { "Row1-Column1", "Row1-Column2", new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nexticonsmall.gif") },
			//		{ "Row2-Column1", "Row2-Column2", new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nexticonsmall.gif") } };
			Vector<String> columnNames = new Vector<String>();
			columnNames.add("Alias");
			columnNames.add("Type");
//			columnNames.add("");
			
			if (!rowData.isEmpty()) {
				DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
			
			jTable = new JTable(model){
				//  Returning the Class of each column will allow different
				//  renderers to be used based on Class
				public Class getColumnClass(int column)
				{
					return getValueAt(0, column).getClass();
				}
			};
			} else {
				jScrollPane.setVisible(false);
			}
		}
		
		count = jTable.getModel().getRowCount();
		jLabel1.setText((String) labels.get("jLabel1") + " " + count);
		jLabel1.setFont(new java.awt.Font("Calibri", Font.ITALIC, 14));
		ImageIcon image = new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nexticonsmall.gif");
		jLabel1.setIcon(image);
		JTableHeader baslik = jTable.getTableHeader();
		baslik.setForeground(Color.white);
		baslik.setBackground(generate);
		baslik.setFont(new Font("Arial", Font.BOLD, 14));
		
		return jTable;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
